﻿Imports Oracle.DataAccess.Client
Public Class fListaProductos
    Dim lIdArticulo As String
    Dim lestado As Int16 = 0
    Dim lItemBusca As String
    Dim lListaPrecios As String = ""
    Dim lContenido As Integer
    'Dim lDescripcion As String
    Public Property ListaPrecios() As String
        Get
            Return lListaPrecios
        End Get
        Set(ByVal value As String)
            lListaPrecios = value
        End Set
    End Property
    Public ReadOnly Property idArticulo() As String
        Get
            Return lIdArticulo
        End Get
    End Property
    Public ReadOnly Property mcontenido() As Integer
        Get
            Return lContenido
        End Get
    End Property
    Public WriteOnly Property ItemBusca() As String
        Set(ByVal value As String)
            lItemBusca = value
        End Set
    End Property
    'Public ReadOnly Property pDescrip() As String
    '    Get
    '        Return lDescripcion
    '    End Get
    'End Property
    Public WriteOnly Property estado() As Int16
        Set(ByVal value As Int16)
            Select Case value
                Case 1
                    Me.rbActivos.Checked = True
                Case 2
                    Me.rbInactivos.Checked = True
                Case Else
                    Me.rbTodos.Checked = True
            End Select
        End Set
    End Property
    Private Sub Consultar()
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim entro As Integer = 0
        If lListaPrecios.Trim.Length > 0 Then
            cmd.CommandText = "SELECT * FROM PRO_PRODUCTO, PRO_DETALLELISTAPRECIOS WHERE PRO_PRODUCTO.IDPRODUCTO = PRO_DETALLELISTAPRECIOS.IDPRODUCTO AND IDLISTA = :PIDLISTA"
            cmd.Parameters.Add("PIDLISTA", OracleDbType.Varchar2).Value = lListaPrecios
            entro = 1
        Else
            cmd.CommandText = "SELECT * FROM PRO_PRODUCTO "
        End If
        If Me.txtDescripcion.Text.Trim.Length > 0 Then
            If entro = 0 Then
                cmd.CommandText &= " WHERE UPPER(DESCRICION) LIKE :PPRODUCTO "
                entro = 1
            Else
                cmd.CommandText &= " AND UPPER(DESCRICION) LIKE :PPRODUCTO"
            End If
            cmd.Parameters.Add("PPRODUCTO", OracleDbType.Varchar2, 40).Value = "%" & Me.txtDescripcion.Text.ToUpper & "%"
        End If
        If Me.txtReferencia.Text.Trim.Length > 0 Then
            If entro = 0 Then
                cmd.CommandText &= " WHERE REFERENCIA = :PREFERENCIA "
                entro = 1
            Else
                cmd.CommandText &= " AND REFERENCIA = :PREFERENCIA "
            End If
            cmd.Parameters.Add("PREFERENCIA", OracleDbType.Varchar2).Value = Me.txtReferencia.Text
        End If
        If lestado = 1 Or lestado = 2 Then
            If entro = 0 Then
                cmd.CommandText &= " WHERE ESTADO=:PESTADO"
                entro = 1
            Else
                cmd.CommandText &= "AND ESTADO=:PESTADO"
            End If
            cmd.Parameters.Add("PESTADO", OracleDbType.Int16).Value = lestado
        End If
        Try
            DsProductos.PRO_PRODUCTO.Rows.Clear()
            conn.Open()
            DsProductos.PRO_PRODUCTO.Load(cmd.ExecuteReader)
            PRO_PRODUCTODataGridView.Focus()
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Consultar()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
    Private Sub AsignaProducto(ByVal Fila As Integer)
        If Fila >= 0 Then
            lIdArticulo = PRO_PRODUCTODataGridView.Rows(Fila).Cells("IDPRODUCTO").Value
            lContenido = PRO_PRODUCTODataGridView.Rows(Fila).Cells("DataGridViewTextBoxColumn4").Value

            'lDescripcion = PRO_PRODUCTODataGridView.Rows(Fila).Cells("DESCRICION").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub
    Private Sub PRO_PRODUCTODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles PRO_PRODUCTODataGridView.CellMouseDoubleClick
        AsignaProducto(e.RowIndex)
    End Sub

    Private Sub PRO_PRODUCTODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PRO_PRODUCTODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaProducto(PRO_PRODUCTODataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub fListaProductos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If lItemBusca IsNot Nothing AndAlso lItemBusca.Trim.Length > 0 Then
            Me.txtDescripcion.Text = lItemBusca
            Consultar()
        End If
    End Sub

    Private Sub txtDescripcion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescripcion.KeyDown
        Try
            Select Case e.KeyCode
                Case 13
                    Consultar()
                Case Else
            End Select
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class